﻿using System;
using System.Drawing;

namespace syc
{
    public partial class ImgCode : System.Web.UI.Page
    {
        private const string Codes = "0123456789abcdehkmnx";
        private const int ImgWidth = 60;
        private const int ImgHeight = 24;
        protected void Page_Load(object sender, EventArgs e)
        {
            TimeSpan ts = new TimeSpan(0, 0, 0);
            Response.Cache.SetMaxAge(ts);
            string code = "";
            Bitmap img = new Bitmap(ImgWidth, ImgHeight);
            Graphics g = Graphics.FromImage(img);
            SolidBrush brush = new SolidBrush(Color.FromArgb(255, 0xdd, 0x33, 0x11));
            Font font = new Font("System", 16, FontStyle.Regular);
            Pen pen = new Pen(brush, 1);
            int i, j;
            Random rnd = new Random();
            for (i = 0; i < 4; i++)
            {
                code = code + Codes.Substring(rnd.Next() % 20, 1);
            }
            g.Clear(Color.FromArgb(255,0xf7,0xf1,0xf0));
            for (i = 0; i < ImgWidth; i += 1)
            {
                for (j = 0; j < ImgHeight; j += 1)
                {
                    if (rnd.Next() % 60 == 0)
                    {
                        g.DrawEllipse(pen, i, j, 1, 1);
                    }
                }
            }
            g.DrawString(code, font, brush, 0, 0);
            Session["ImgCode"] = code;
            Response.ContentType = "image/gif";
            img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
            img.Dispose();
            g.Dispose();
        }
    }
}
